//
//  UIImageVIew+ImageEffects.m
//  IOS_FrameWork_Du
//
//  Created by zuibaxian on 8/18/14.
//  Copyright (c) 2014 duyiming. All rights reserved.
//

#import "UIImage+ImageEffects.h"
#import <CoreImage/CoreImage.h>

@implementation UIImage (ImageEffects)

-(void)blurWithRadius:(CGFloat)blurRadius block:(void (^)(UIImage *))block
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^(void){
        CIImage *inputImage = [[CIImage alloc] initWithCGImage:self.CGImage];
        //获取上下文
        CIContext *context = [CIContext contextWithOptions:nil];
        CGAffineTransform transform = CGAffineTransformIdentity;
        //高斯模糊时图片内容会变小,故使用此滤镜
        CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
        [clampFilter setValue:inputImage forKey:kCIInputImageKey];
        [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
        
        CIImage *outputImage = [clampFilter outputImage];
        
        //高斯模糊
        CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"
                                          keysAndValues:kCIInputImageKey, outputImage, @"inputRadius", [NSNumber numberWithFloat:blurRadius], nil];
        
        outputImage = [blurFilter outputImage];
        
        //增强饱和度
        CIFilter *colorContrlsFilter = [CIFilter filterWithName:@"CIColorControls"
                                          keysAndValues:kCIInputImageKey, outputImage, @"inputSaturation", [NSNumber numberWithFloat:2], nil];
        outputImage = [colorContrlsFilter outputImage];
        CGImageRef cgimg = [context createCGImage:outputImage fromRect:[inputImage extent]];
        UIImage *blurredImage = [UIImage imageWithCGImage:cgimg];
        CGImageRelease(cgimg);
        dispatch_async(dispatch_get_main_queue(), ^(void){
            if (block) {
                block(blurredImage);
            }
        });
    });
}

-(UIImage *)testBlurWithRadius:(CGFloat)blurRadius
{
    CIImage *inputImage = [[CIImage alloc] initWithCGImage:self.CGImage];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    [clampFilter setValue:inputImage forKey:kCIInputImageKey];
    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
    
    CIImage *outputImage = [clampFilter outputImage];
    
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"
                                      keysAndValues:kCIInputImageKey, outputImage, @"inputRadius", [NSNumber numberWithFloat:blurRadius], nil];
    
    outputImage = [blurFilter outputImage];
    
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[inputImage extent]];
    UIImage *blurredImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    return blurredImage;
}

-(UIImage *)cutImageInSize:(CGSize)size
{
    return nil;
}

@end
